package com.ganzalang.gmall.designpatterns.builder;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用构造器模式
 * @author: ganzalang
 */
@Slf4j
public class BuilderPatternDemo {

    public static void main(String[] args) {
        Product product = new ProductBuilder().field1("value1")
                .field2("value2")
                .field3("value3")
                .builder();
        log.info("product: {}", product);
    }
    @Data
    public static class Product {
        private String field1;
        private String field2;
        private String field3;
    }

    public interface Builder {
        Builder field1(String field1);
        Builder field2(String field2);
        Builder field3(String field3);
        Product builder();
    }

    public static class ProductBuilder implements Builder {

        private Product product = new Product();

        @Override
        public Builder field1(String field1) {
            // ...可以做一些处理
            product.setField1(field1);
            return this;
        }

        @Override
        public Builder field2(String field2) {
            // ...可以做一些处理
            product.setField2(field2);
            return this;
        }

        @Override
        public Builder field3(String field3) {
            // ...可以做一些处理
            product.setField3(field3);
            return this;
        }

        @Override
        public Product builder() {
            return product;
        }
    }
}
